package facepp

import (
	"fmt"
	"time"

	"github.com/go-resty/resty/v2"
)

// type FaceDetectReq struct {
// 	Image_url         string
// 	Image_file        string
// 	Image_base64      string
// 	Return_landmark   int
// 	Return_attributes string
// 	Calculate_all     string
// 	Face_rectangle    string
// 	Beauty_score_min  int
// 	Beauty_score_max  int
// }

type FaceDetectFace struct {
	Face_token     string `json:"face_token"`
	Face_rectangle struct {
		Left   int `json:"left"`
		Top    int `json:"top"`
		Width  int `json:"width"`
		Height int `json:"height"`
	} `json:"face_rectangle"`
}

type FaceDetectResp struct {
	Request_id    string           `json:"request_id"`
	Faces         []FaceDetectFace `json:"faces"`
	Image_id      string           `json:"image_id"`
	Time_used     int              `json:"time_used"`
	Error_message string           `json:"error_message"`
	Face_num      int              `json:"face_num"`
}

// 人脸检测：通过图片的base64编码数据进行人脸检测
func (f *FacePP) FaceDetectByBase64(imageBase64, returnLandmark, returnAttributes string) (*FaceDetectResp, error) {
	if imageBase64 == "" {
		return nil, fmt.Errorf(CallApiErrFormat2, ParamsInvalid.Code, fmt.Sprintf(ParamsInvalid.Message, "imageBase64"))
	}
	params := make(map[string]string)
	params["api_key"] = f.ApiKey
	params["api_secret"] = f.ApiSecret
	params["image_base64"] = imageBase64
	if returnLandmark != "" {
		params["return_landmark"] = returnLandmark
	}
	if returnAttributes != "" {
		params["return_attributes"] = returnAttributes
	}

	var faceDetectResp FaceDetectResp
	var apiErrResp ApiErrResp
	client := resty.New()
	client.SetTimeout(10 * time.Second)
	resp, err := client.R().
		SetHeader(ACCEPT, APP_JSON).
		SetFormData(params).
		SetResult(&faceDetectResp).
		SetError(&apiErrResp).
		Post(FACE_DETECT_URL)
	if err != nil {
		return nil, fmt.Errorf(CallApiErrFormat2, HttpClientReqError.Code, HttpClientReqError.Message)
	}
	if resp.StatusCode() != 200 {
		return nil, fmt.Errorf(CallApiErrFormat2, resp.StatusCode(), apiErrResp.Error_message)
	}
	return &faceDetectResp, nil
}

// 人脸检测：通过图片文件进行人脸检测
func (f *FacePP) FaceDetectByImageFile(filepath, returnLandmark, returnAttributes string) (*FaceDetectResp, error) {
	if filepath == "" {
		return nil, fmt.Errorf(CallApiErrFormat2, ParamsInvalid.Code, fmt.Sprintf(ParamsInvalid.Message, "filepath"))
	}
	params := make(map[string]string)
	params["api_key"] = f.ApiKey
	params["api_secret"] = f.ApiSecret
	if returnLandmark != "" {
		params["return_landmark"] = returnLandmark
	}
	if returnAttributes != "" {
		params["return_attributes"] = returnAttributes
	}

	var faceDetectResp FaceDetectResp
	var apiErrResp ApiErrResp
	client := resty.New()
	client.SetTimeout(10 * time.Second)
	resp, err := client.R().
		SetHeader(ACCEPT, APP_JSON).
		SetFile("image_file", filepath).
		SetFormData(params).
		SetResult(&faceDetectResp).
		SetError(&apiErrResp).
		Post(FACE_DETECT_URL)
	if err != nil {
		return nil, fmt.Errorf(CallApiErrFormat2, HttpClientReqError.Code, HttpClientReqError.Message)
	}
	if resp.StatusCode() != 200 {
		return nil, fmt.Errorf(CallApiErrFormat2, resp.StatusCode(), apiErrResp.Error_message)
	}
	return &faceDetectResp, nil
}

// 人脸检测：通过图片的url进行人脸检测
func (f *FacePP) FaceDetectByImageUrl(imageUrl, returnLandmark, returnAttributes string) (*FaceDetectResp, error) {
	if imageUrl == "" {
		return nil, fmt.Errorf(CallApiErrFormat2, ParamsInvalid.Code, fmt.Sprintf(ParamsInvalid.Message, "imageUrl"))
	}
	params := make(map[string]string)
	params["api_key"] = f.ApiKey
	params["api_secret"] = f.ApiSecret
	params["image_url"] = imageUrl
	if returnLandmark != "" {
		params["return_landmark"] = returnLandmark
	}
	if returnAttributes != "" {
		params["return_attributes"] = returnAttributes
	}

	var faceDetectResp FaceDetectResp
	var apiErrResp ApiErrResp
	client := resty.New()
	client.SetTimeout(10 * time.Second)
	resp, err := client.R().
		SetHeader(ACCEPT, APP_JSON).
		SetFormData(params).
		SetResult(&faceDetectResp).
		SetError(&apiErrResp).
		Post(FACE_DETECT_URL)
	if err != nil {
		return nil, fmt.Errorf(CallApiErrFormat2, HttpClientReqError.Code, HttpClientReqError.Message)
	}
	if resp.StatusCode() != 200 {
		return nil, fmt.Errorf(CallApiErrFormat2, resp.StatusCode(), apiErrResp.Error_message)
	}
	return &faceDetectResp, nil
}
